package com.bluesky.jwf.example1.forms;

import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Button;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.Label;
import com.bluesky.jwf.component.LongTextField;
import com.bluesky.jwf.component.captcha.Captcha;
import com.bluesky.jwf.component.captcha.CaptchaService;
import com.bluesky.jwf.component.captcha.CaptchaServiceProvider;
import com.bluesky.jwf.component.captcha.CaptchasDotNetService;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;
import com.bluesky.jwf.component.validator.CaptchaValidator;
import com.bluesky.jwf.component.validator.EmailValidator;
import com.bluesky.jwf.component.validator.GroupValidator;
import com.bluesky.jwf.component.validator.RequiredValidator;
import com.bluesky.jwf.component.window.Dialog;
import com.bluesky.jwf.component.window.ModalResult;

public class QuestionDialog extends Dialog {

	private Label questionLabel;
	private LongTextField answerTextBox;
	private RequiredValidator answerRequiredValidator;
	private GroupValidator answerGroupValidator;
	private Button buttonOK;
	private Button buttonCancel;

	@Override
	public void init(String html) {

		super.init(html);

		questionLabel = (Label) ComponentFactory.getInstance().createComponent(
				"Label");
		loadByJwfId("question").replace(questionLabel);

		answerTextBox = (LongTextField) ComponentFactory.getInstance()
				.createComponent("LongTextField");

		loadByJwfId("answer").replace(answerTextBox);

		answerRequiredValidator = (RequiredValidator) ComponentFactory
				.getInstance().createComponent("RequiredValidator");
		answerRequiredValidator.setTarget(answerTextBox);

		EmailValidator answerEmailValidator = (EmailValidator) ComponentFactory
				.getInstance().createComponent("EmailValidator");
		answerEmailValidator.setTarget(answerTextBox);

		answerGroupValidator = (GroupValidator) ComponentFactory.getInstance()
				.createComponent("GroupValidator");
		answerGroupValidator.setTarget(answerTextBox);

		answerGroupValidator.addValidator(answerRequiredValidator);
		answerGroupValidator.addValidator(answerEmailValidator);
		bodyTag.loadByJwfClass("answerValidator").replace(answerGroupValidator);

		Captcha captcha = (Captcha) ComponentFactory.getInstance()
				.createComponent(Captcha.class.getSimpleName());
		captcha.setServiceProvider(new CaptchaServiceProvider() {
			@Override
			public CaptchaService getService() {

				// return new MathCaptchaService();
				return CaptchasDotNetService.getInstance();
			}
		});

		bodyTag.loadByJwfClass("captchaContainer").addChild(captcha);

		CaptchaValidator captchaValidator = (CaptchaValidator) ComponentFactory
				.getInstance().createComponent("CaptchaValidator");
		captchaValidator.setTarget(captcha);
		bodyTag.loadByJwfClass("captchaContainer").addChild(captchaValidator);

		buttonOK = (Button) ComponentFactory.getInstance().createComponent(
				"Button");
		buttonOK.setText("OK");
		loadByJwfId("ok").replace(buttonOK);

		buttonOK.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter ) {
				validate();
				if (!validateResult)
					return;

				modalResult = ModalResult.OK;

				setAnswer(answerTextBox.getText());

				close();

			}
		});

		buttonCancel = (Button) ComponentFactory.getInstance().createComponent(
				"Button");
		buttonCancel.setText("Cancel");
		loadByJwfId("cancel").replace(buttonCancel);
		buttonCancel.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter ) {

				modalResult = ModalResult.Cancel;

				close();

			}
		});

	}

	public String getQuestion() {
		return questionLabel.getText();
	}

	public void setQuestion(String question) {
		questionLabel.setText(question);
	}

	public String getAnswer() {
		return answerTextBox.getText();
	}

	public void setAnswer(String answer) {
		this.answerTextBox.setText(answer);
	}

	public Label getQuestionLabel() {
		return questionLabel;
	}

	public void setQuestionLabel(Label questionLabel) {
		this.questionLabel = questionLabel;
	}

	public Button getButtonOK() {
		return buttonOK;
	}

	public void setButtonOK(Button buttonOK) {
		this.buttonOK = buttonOK;
	}

	public Button getButtonCancel() {
		return buttonCancel;
	}

	public void setButtonCancel(Button buttonCancel) {
		this.buttonCancel = buttonCancel;
	}

}
